//
//  CAHistoryDataNALG.m
//  sr01sdkProject
//
//  Created by  on 2024/9/2.
//

#import "CAHistoryDataNALG.h"

@implementation CAHistoryDataNALG

-(NSNumber *)dataTimeStamp
{
    return self.timeStamp;
}

-(instancetype)initWithData:(NSData *)data
{
    if (self = [super init])
    {
        if (data.length < 20) {
            return self;
        }
        uint8_t *dataBytes = (uint8_t *)[data bytes];

        uint32_t timeStamp = *((uint32_t *)(dataBytes + 2));
        _timeStamp = @(timeStamp);
        
        uint32_t uuid = (dataBytes[8]<< 16) +(dataBytes[7]<< 8) +dataBytes[6];
        _dataUuid = @(uuid);

        uint16_t ibiTmp =  *((uint16_t *)(dataBytes + 9)) ;
        if (ibiTmp != 0xFFFF) {
            _IBI =  @(ibiTmp);
        }

        uint32_t stressTmp = dataBytes[11] & 0xFF;
//        DebugNSLog(@"cm数据 压力:%d", stressTmp);
        if (dataBytes[11] != 0xFF ) {
            _stress = @((dataBytes[11] & 0xFF) / 100.0f);
        }
        
        uint8_t cardiacCoherence = dataBytes[12]& 0XFF ;
        
        if (cardiacCoherence <= 100  ) {// 0xff无效值
            _cardiacCoherence = @(cardiacCoherence /100.0f);
        }
        
        uint16_t hrvTmp = *((uint16_t *)(dataBytes + 13));
        if (hrvTmp != 0XFFFF) {
            _hrv = @(hrvTmp) ;
        }
        
        uint8_t br = dataBytes[15] & 0xFF;
        if (br > 0 && br != 0XFF) { // 0 为不存储数值
            _breathRate = @(dataBytes[15] & 0xFF);
        }
        uint8_t restHr = dataBytes[17]; // 静息心率
        if (restHr != 0xFF && restHr != 0) {
            _restHr = @(restHr);
        }
        
    }
    return self;
}
@end
